package com.hepinghospital.user.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;

import java.util.Objects;

/**
 * @Description: 护理预约 服务申请信息 选择弹框
 * @Author: lzp
 * @CreateDate: 2022/3/22 14:47
 * @UpdateRemark: 更新说明
 */
public class Choose2Dialog extends BaseDialogFragment {

    public static Choose2Dialog newInstance(String select) {
        Bundle args = new Bundle();
        args.putString("select", select);
        Choose2Dialog fragment = new Choose2Dialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_confirm, btn_cancel, tv_message;

    private String message;
    private String confirm;
    private String cancel;
    private String select;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_choose2, container);
        btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        tv_message = view.findViewById(R.id.tv_message);
        setInfo();
        btn_cancel.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }
        });

        btn_confirm.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onConfirm();
                dismiss();
            }
        });

        if (getArguments() != null) {
            select = getArguments().getString("select");
        }
        if (select.equals(confirm)) {
            btn_confirm.setTextColor(ContextCompat.getColor(requireContext(), R.color.white));
            btn_confirm.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.selector_button));
            btn_cancel.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonGrey));
            btn_cancel.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.shape_grey));
        } else if (select.equals(cancel)) {
            btn_confirm.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonGrey));
            btn_confirm.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.shape_grey));
            btn_cancel.setTextColor(ContextCompat.getColor(requireContext(), R.color.white));
            btn_cancel.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.selector_button));
        } else {
            btn_confirm.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonGrey));
            btn_confirm.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.shape_grey));
            btn_cancel.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonGrey));
            btn_cancel.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.shape_grey));
        }
        return view;
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    public void setText(String message, String confirm, String cancel) {
        this.message = message;
        this.confirm = confirm;
        this.cancel = cancel;
    }


    public void setInfo() {
        tv_message.setText(message);
        btn_confirm.setText(confirm);
        btn_cancel.setText(cancel);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
